package dto

import (
	"time"
	"tuowei.com/app/admin/models"
	"tuowei.com/common/dto"
	common "tuowei.com/common/models"
)

type SysStudentGetPageReq struct {
	dto.Pagination `search:"-"`
	//Keywords       string `form:"keywords" search:"type:contains;column:name;table:sys_student" comment:"学生名称关键字"`     // 课程名称
	Phone     string `form:"phone" search:"type:contains;column:phone;table:sys_student" comment:"学生名称关键字"`       // 手机号码关键字
	NickName  string `form:"nick_name" search:"type:contains;column:nick_name;table:sys_student" comment:"昵称关键字"` // 昵称关键字
	Status    int    `form:"status" search:"type:exact;column:status;table:sys_student" comment:"状态"`             // 状态
	StartTime string `search:"type:gte;column:created_at;table:sys_student" form:"beginTime"`
	EndTime   string `search:"type:lte;column:created_at;table:sys_student" form:"endTime"`
	Name      string `search:"type:exact;column:name;table:sys_student" form:"name" json:"name"`
	IdCard    string `search:"type:exact;column:id_card;table:sys_student" form:"id_card" json:"id_card"`
	From      int    `search:"-" form:"from" json:"from"` //来源 1-自己来的，2-学生邀请的，3-教师邀请的
	UserType  string `search:"type:exact;column:user_type;table:sys_student" form:"user_type" json:"user_type"`
	UserLabel string `search:"type:exact;column:user_label;table:sys_student" form:"user_label" json:"user_label"`
	Province  int    `search:"type:exact;column:province;table:sys_student" form:"province" json:"province"`
	Inviter   string `search:"-" form:"inviter" json:"inviter"` //邀请人名字（可能是老师，可能是学生）
	SysStudentOrder
}

type SysStudentOrder struct {
	StudentIdOrder string `search:"type:order;column:student_id;table:sys_student" form:"student_id_sort"`
	CreatedAtOrder string `search:"type:order;column:created_at;table:sys_student" form:"created_at_sort"`
	UpdatedAtOrder string `search:"type:order;column:updated_at;table:sys_student" form:"updated_at_sort"`
}

func (m *SysStudentGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type SysStudentInsertReq struct {
	Id    int    `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"` // 学员ID
	Name  string `json:"name" gorm:"type:varchar(50);not null;comment:学员名称"`
	Phone string `json:"phone" gorm:"type:varchar(20);not null;comment:手机号码"`
	//TotalPay            float32 `json:"total_pay" gorm:"type:decimal(10,2);comment:付费总额"`
	UserLabel string `json:"user_label" gorm:"type:varchar(50);comment:学员标识 线上/线下"`
	//OrderCount          int     `json:"order_count" gorm:"type:int(10);comment:订单总数"`
	UserType            string `json:"user_type" gorm:"tinyint(1);comment:用户类型 普通/VIP/SVIP"`
	Province            int    `json:"province" gorm:"type:int(10);comment:所属大区"`
	City                int    `json:"city" gorm:"type:int(10);comment:所属区域"`
	Area                int    `json:"area" gorm:"type:int(10);comment:所属教学点"`
	ShenBenTime         string `json:"shen_ben_time"`
	Recommender         int    `json:"recommender" gorm:"type:int(10);comment:推荐人"`
	RmId                int    `json:"rm_id" gorm:"type:smallint(2);comment:大区经理ID"`
	RegionalMId         int    `json:"regional_m_id" gorm:"type:smallint(2);comment:区域经理ID"`
	EnrollmentTeacherId int    `json:"enrollment_teacher_id" gorm:"type:smallint(2);comment:招生老师ID"`
	ClassId             int    `json:"class_id" gorm:"type:smallint(2);comment:班级ID"`
	Source              string `json:"source" gorm:"type:varchar(20);comment:学员来源"`
	Status              int    `json:"status" gorm:"type:tinyint(1);comment:学员状态"`
	common.ModelTime
}

func (s SysStudentInsertReq) Generate(student *models.SysStudent) {
	student.Name = s.Name
	student.Province = s.Province
	student.SerialNum = "TW-STUDENT-" + time.Now().Format("20060102150405")
	student.City = s.City
	student.Source = s.Source
	student.Recommender = s.Recommender
	student.Phone = s.Phone
	student.CreatedAt = s.CreatedAt
	student.ShenBenTime = TimeStringToGoTime(s.ShenBenTime)
	student.UserLabel = s.UserLabel
	student.UserType = s.UserType
	student.Area = s.Area
	student.RmId = s.RmId
	student.RegionalMId = s.RegionalMId
	student.EnrollmentTeacherId = s.EnrollmentTeacherId
	student.ClassId = s.ClassId
}

func (s *SysStudentInsertReq) GetId() interface{} {
	return s.Id
}

type SysStudentUpdateStatusReq struct {
	Status    int `json:"status" comment:"状态"`
	StudentId int `json:"student_id" comment:"课程ID" vd:"$>0; msg:'课程ID不能为空'"`
	common.ControlBy
}

func (s SysStudentUpdateStatusReq) Generate(model *models.SysStudent) {
	model.Status = s.Status
}

func (s SysStudentUpdateStatusReq) GetId() int {
	return s.StudentId
}

type UpdateStudentStatusReq struct {
	RoleId int    `json:"roleId" comment:"角色编码"` // 角色编码
	Status string `json:"status" comment:"状态"`   // 状态
	common.ControlBy
}

func (s *UpdateStudentStatusReq) Generate(model *models.SysRole) {
	if s.RoleId != 0 {
		model.RoleId = s.RoleId
	}
	model.Status = s.Status
}

func (s *UpdateStudentStatusReq) GetId() interface{} {
	return s.RoleId
}

type SysStudentByName struct {
	RoleName string `form:"role"` // 角色编码
}

type SysStudentGetReq struct {
	Id int `uri:"id"`
}

func (s *SysStudentGetReq) GetId() interface{} {
	return s.Id
}

type SysStudentDeleteReq struct {
	Ids []int `json:"ids"`
}

func (s *SysStudentDeleteReq) GetId() interface{} {
	return s.Ids
}

// RoleDataScopeReq 角色数据权限修改
type StudentDataScopeReq struct {
	RoleId    int    `json:"roleId" binding:"required"`
	DataScope string `json:"dataScope" binding:"required"`
	DeptIds   []int  `json:"deptIds"`
}

func (s *StudentDataScopeReq) Generate(model *models.SysRole) {
	if s.RoleId != 0 {
		model.RoleId = s.RoleId
	}
	model.DataScope = s.DataScope
	model.DeptIds = s.DeptIds
}

type SysStudentById struct {
	dto.ObjectById
	common.ControlBy
}

func (m *SysStudentById) GetNeedSearch() interface{} {
	return *m
}

type SysStudentUpdateBuyStatusReq struct {
	Ids       []int `json:"ids" binding:"required"`
	BuyStatus int   `json:"buy_status" binding:"required"`
}

type SysStudentExcelReq struct {
	StartTime string `json:"start_time" form:"start_time" binding:"required"`
	EndTime   string `json:"end_time" form:"end_time" binding:"required"`
}

type GetListByClassIdReq struct {
	dto.Pagination `search:"-"`
	ClassId        int `json:"class_id" form:"class_id" search:"type:exact;column:class_id;table:sys_student" binding:"required"`
}

type GetNoClassStudentListReq struct {
	dto.Pagination `search:"-"`
}

func (m *GetListByClassIdReq) GetNeedSearch() interface{} {
	return *m
}

type SysStudentUpdateReq struct {
	common.Model
	Name                string    `json:"name" gorm:"type:varchar(50);not null;comment:学员名称"`
	CollegeId           int       `json:"college_id" gorm:"type:int(5);not null;default:0;comment:学员院校ID"`
	NickName            string    `json:"nick_name" gorm:"type:varchar(100);comment:昵称"`
	Phone               string    `json:"phone" gorm:"type:varchar(20);not null;comment:手机号码"`
	TotalPay            float32   `json:"total_pay" gorm:"type:decimal(10,2);comment:付费总额"`
	UserLabel           string    `json:"user_label" gorm:"type:varchar(50);comment:学员标识 线上/线下"`
	OrderCount          int       `json:"order_count" gorm:"type:int(10);comment:订单总数"`
	UserType            string    `json:"user_type" gorm:"varchar(10);comment:用户类型 普通/VIP/SVIP"`
	Province            int       `json:"province" gorm:"type:int(10);comment:所属大区"`
	City                int       `json:"city" gorm:"type:int(10);comment:所属区域"`
	Area                int       `json:"area" gorm:"type:int(10);comment:所属教学点"`
	ShenBenTime         string    `json:"shen_ben_time"`
	LastPayTime         time.Time `json:"last_pay_time"`
	Recommender         int       `json:"recommender" gorm:"type:int(10);comment:推荐人"`
	RmId                int       `json:"rm_id" gorm:"type:smallint(2);comment:大区经理ID"`
	RegionalMId         int       `json:"regional_m_id" gorm:"type:smallint(2);comment:区域经理ID"`
	EnrollmentTeacherId int       `json:"enrollment_teacher_id" gorm:"type:smallint(2);comment:招生老师ID"`
	ClassId             int       `json:"class_id" gorm:"type:smallint(2);comment:班级ID"`
	Source              string    `json:"source" gorm:"type:varchar(20);comment:学员来源"`
	Status              int       `json:"status" gorm:"type:tinyint(1);comment:学员状态"`
	ProgressStatus      int       `json:"progress_status" gorm:"type:tinyint(1);not null;default:1;comment:学员跟进状态，1-刚添加的学生，2-待联系，3-待分班，4-完成"`
	Faculty             string    `json:"faculty" gorm:"type:varchar(20);comment:院系"`
	Major               string    `json:"major" gorm:"type:varchar(50);comment:专业"`
	common.ModelTime
}

func (s *SysStudentUpdateReq) Generate(model *models.SysStudent) {
	if s.Id != 0 {
		model.Id = s.Id
	}
	model.Name = s.Name
	model.Province = s.Province
	model.City = s.City
	model.Source = s.Source
	model.Recommender = s.Recommender
	model.Phone = s.Phone
	model.CreatedAt = s.CreatedAt
	model.ShenBenTime = TimeStringToGoTime(s.ShenBenTime)
	model.UserLabel = s.UserLabel
	model.UserType = s.UserType
	model.Area = s.Area
	model.RmId = s.RmId
	model.RegionalMId = s.RegionalMId
	model.EnrollmentTeacherId = s.EnrollmentTeacherId
	model.ClassId = s.ClassId

}

var timeTemplates = []string{
	"2006-01-02 15:04:05", //常规类型
	"2006/01/02 15:04:05",
	"2006-01-02",
	"2006/01/02",
	"15:04:05",
}

/* 时间格式字符串转换 */
func TimeStringToGoTime(tm string) time.Time {
	for i := range timeTemplates {
		t, err := time.ParseInLocation(timeTemplates[i], tm, time.Local)
		if nil == err && !t.IsZero() {
			return t
		}
	}
	return time.Time{}
}

func (s *SysStudentUpdateReq) GetId() interface{} {
	return s.Id
}
